﻿using System;
using System.IO;
using System.Xml;
using System.Resources;
using System.Reflection;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using MPMovieCatalog.Configuration;

namespace MPMovieCatalog.Util
{
    public class ResourceUtils
    {
        private static Dictionary<string, LanguageFile> m_languages = null;

        private static string m_currentLanguage;

        private const string DEFAULT_LANGUAGE = "English";

        public static List<LanguageFile> GetLanguages()
        {
            List<LanguageFile> languages = new List<LanguageFile>();
            foreach (LanguageFile languageFile in m_languages.Values)
            {
                languages.Add(languageFile);
            }
            return languages;
        }

        public static string GetString(string sectionName, long id)
        {
            string str = m_languages[GetCurrentLanguage()].GetString(sectionName, id);
            if (string.IsNullOrEmpty(str))
            {
                str = m_languages[DEFAULT_LANGUAGE].GetString(sectionName, id);
            }

            return str;
        }

        public static Dictionary<string, string> GetSection(string sectionName)
        {
            Dictionary<string, string> section = m_languages[GetCurrentLanguage()].GetSection(sectionName);
            if (section == null || section.Count == 0)
            {
                section = m_languages[DEFAULT_LANGUAGE].GetSection(sectionName);
            }
            return section;
        }

        public static void Refresh()
        {
            ConfigurationWrapper wrapper = new ConfigurationWrapper();
            m_currentLanguage = wrapper.GUISection.Language;

            if (m_languages == null)
            {
                m_languages = new Dictionary<string, LanguageFile>(); ;

                Directory.CreateDirectory(Application.StartupPath + @"\language");
                DirectoryInfo di = new DirectoryInfo(Application.StartupPath + @"\language");
                FileInfo[] fileList = di.GetFiles("*.xml", SearchOption.AllDirectories);
                foreach (FileInfo f in fileList)
                {
                    XmlDocument document = new XmlDocument();
                    document.Load(f.FullName);

                    XmlNode root = document.DocumentElement;
                    if (root.Name == "language")
                    {
                        string name = root.Attributes["name"].Value;

                        LanguageFile languageFile = null;
                        if (!m_languages.TryGetValue(name, out languageFile))
                        {
                            languageFile = new LanguageFile(name, f.FullName);
                            m_languages[name] = languageFile;
                        }
                    }
                }
            }
        }

        public static string GetCurrentLanguage()
        {
            if (string.IsNullOrEmpty(m_currentLanguage))
            {
                Refresh();
            }
            return m_currentLanguage;
        }
    }

    public class LanguageFile
    {
        private Dictionary<string, Dictionary<string, string>> m_sections = null;

        public LanguageFile(string language, string fileName)
        {
            Language = language;
            FileName = fileName;
        }

        public string GetString(string sectionName, long id)
        {
            if (m_sections == null)
            {
                LoadFile();
            }

            Dictionary<string, string> section = null;
            if (m_sections.TryGetValue(sectionName, out section))
            {
                string resource;
                if (section.TryGetValue(id.ToString(), out resource))
                {
                    return resource;
                }
            }

            return "";
        }

        public Dictionary<string, string> GetSection(string sectionName)
        {
            if (m_sections == null)
            {
                LoadFile();
            }

            Dictionary<string, string> section = null;
            if (m_sections.TryGetValue(sectionName, out section))
            {
                return section;
            }

            return null;
        }

        private void LoadFile()
        {
            m_sections = new Dictionary<string, Dictionary<string, string>>();

            XmlDocument document = new XmlDocument();
            document.Load(FileName);

            XmlNode root = document.DocumentElement;
            if (root.Name == "language")
            {
                foreach (XmlNode sectionNode in root.ChildNodes)
                {
                    Dictionary<string, string> resources = new Dictionary<string, string>();
                    foreach (XmlNode resourceNode in sectionNode.ChildNodes)
                    {
                        resources[resourceNode.Attributes["id"].Value] = resourceNode.FirstChild.Value;
                    }
                    m_sections[sectionNode.Attributes["name"].Value] = resources;
                }

            }
        }

        public string Language { get; set; }
        public string FileName { get; set; }
    }
}
